package com.lordjoe.sudoku;

/**
 * com.lordjoe.sudoku.ConstraintMessage
 * Message from a constgraint to a cell
 * User: Steve
 * Date: 4/7/12
 */
public class ConstraintMessage {
    public static final ConstraintMessage[] EMPTY_ARRAY = {};

    private final double[] m_Values = new double[SudokuBoard.getBoardSize()];
    private final SudokuConstraint m_Constraint;

    public ConstraintMessage(final SudokuConstraint pConstraint,final SudokuCell cell) {
        m_Constraint = pConstraint;
    }

    public SudokuConstraint getConstraint() {
        return m_Constraint;
    }

    public boolean valueBlocked(int val)
    {
        return m_Values[val] == 0;
    }

    public String toString() {
        String name = getConstraint().toString() + ":" ;
        for(double d : getValues())   {
            name += String.format("%2.1f,",d);
        }
         return name;
     }

    public double[] getValues() {
        return m_Values;
    }

    /**
     * make probabilities sum to 1
     */
    protected void normalize( ) {
        double sum = 0;
        for(double d : m_Values)
            sum += d;
        if(sum == 0)
            return;
        for (int i = 0; i < m_Values.length; i++) {
             m_Values[i] /= sum;

        }
    }

    public String getTypeString() {
        return getConstraint().getType().toString().substring(0,1);
    }
}
